﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="File.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The file.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Services.Dto
{
    using System;
    using System.Runtime.Serialization;

    /// <summary>
    /// The file.
    /// </summary>
    [DataContract]
    public class File
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the content data.
        /// </summary>
        [DataMember]
        public byte[] ContentData { get; set; }

        /// <summary>
        /// Gets or sets the content type.
        /// </summary>
        [DataMember]
        public string ContentType { get; set; }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        [DataMember]
        public Guid ID { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        [DataMember]
        public string Name { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The convert.
        /// </summary>
        /// <param name="file">
        /// The file.
        /// </param>
        /// <returns>
        /// The <see cref="File"/>.
        /// </returns>
        public static File Convert(Models.Base.File file)
        {
            if (file != null)
            {
                var dto = new File
                              {
                                  ID = file.ID, 
                                  Name = file.Name, 
                                  ContentType = file.ContentType, 
                                  ContentData = file.Content.Content
                              };
                return dto;
            }
            
            return null;
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Queries
{
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Base;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The file query.
    /// </summary>
    [DataContract]
    public class FileQuery : IQuery
    {

        [DataMember]
        public bool OnlyFileInfo { get; set; }

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public override IResult Execute(ISession hsession)
        {
            var actioner = new FileActioner(hsession, null);
            return actioner.ExecuteSearchQuery(this);
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Results
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using ServiceLibrary.Services.Dto;

    /// <summary>
    /// The file result.
    /// </summary>
    [DataContract]
    public class FileResult : IResult
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="FileResult"/> class.
        /// </summary>
        public FileResult()
        {
            this.List = new List<File>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        [DataMember]
        public List<File> List { get; set; }

        /// <summary>
        /// Gets or sets the total row count.
        /// </summary>
        [DataMember]
        public int TotalRowCount { get; set; }

        #endregion
    }
}

namespace ServiceLibrary.Services.Commands
{
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Base;
    using ServiceLibrary.Services.Dto;

    using User = ServiceLibrary.Models.Domain.User;

    /// <summary>
    /// The create file command.
    /// </summary>
    [DataContract]
    public class CreateFileCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the new file.
        /// </summary>
        [DataMember]
        public File NewFile { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new FileActioner(hsession, activeUser);
            actioner.CreateFile(this);
        }

        #endregion
    }
}